<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2019-05-24
 * Time: 14:15
 */

namespace Edmmss\Sentry\EasySwoole;

use Raven_Client;

class SentryTarget
{
    /**
     * @var string sentry的dsn
     */
    public $dsn;

    /**
     * @var Raven_Client
     */
    public $sentryClient;

    public function __construct()
    {
        $configInstance = \EasySwoole\EasySwoole\Config::getInstance();
        $sentryConfig = $configInstance->getConf('SENTRY');
        $this->dsn = $sentryConfig['dsn'];

        $this->sentryClient = new Raven_Client($this->dsn);
    }

    /**
     * 将错误日志发送sentry
     *
     * @date   2019-05-24
     * @author edmmss
     * @param \Throwable $throwable
     * @param string $message
     */
    public function export(\Throwable $throwable, $message = '')
    {
        // 异常的。直接写就行了。没有其他参数影响的了
        $message = $message ? $message : $throwable->getMessage();
        $traces = $throwable->getTrace();
        $traceAsString = $throwable->getTraceAsString();

        $data['extra']['fullMessage'] = $throwable->getMessage();
        $data['extra']['traces'] = $traces;
        $data['extra']['traceAsString'] = $traceAsString;

        $this->sentryClient->captureMessage($message, [], $data, $traces);
    }
}